﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fodee.DataEntity;
using Fodee.Controls;

namespace Fodee
{
    public partial class OrderDetail : BaseForm
    {
        private int orderID = 0;
        private Table currentTable;
        FodeeDataSet.v_OrderFoodDataTable dtOrder = new FodeeDataSet.v_OrderFoodDataTable();
        public OrderDetail()
        {
            InitializeComponent();
        }
        public OrderDetail(Table table)
        {
            InitializeComponent();
            currentTable = table;
            this.Text = string.Format("桌台-【"+table.TableName+"】");
        }

        private void OrderDetail_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.Escape:
                    this.Close();
                    break;
                default:
                    FoodSearch fs = new FoodSearch();
                    fs.ShowDialog(this);
                    break;
            }
        }

        public void AddFood(int id, string num, string name, int count, string unit, decimal price, string remark, bool isFree)
        {
            FodeeDataSet.v_OrderFoodRow drOrder = dtOrder.Newv_OrderFoodRow();

            foreach (FodeeDataSet.v_OrderFoodRow dr in dtOrder.Rows)
            {
                if (dr.FoodID == id)
                {
                    if (dr.OrderFoodIsFree == isFree)
                    {
                        if (MessageBox.Show("是否多选一次？"
                            , "已经点选 [" + dr.FoodName + "]"
                            , MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            dr.OrderFoodCount += count;
                            dr.OrderFoodRemarks += remark;
                            this.SetOrderInfo();
                            return;
                        }
                    }
                    continue;
                }
                else
                    continue;
            }
            drOrder.ID = dtOrder.Rows.Count;
            drOrder.FoodName = name;
            drOrder.FoodNum = num;
            drOrder.FoodPrice = price;
            drOrder.FoodUnitName = unit;
            drOrder.FoodID = id;
            drOrder.OrderID = this.orderID;
            drOrder.OrderFoodCount = count;
            drOrder.OrderFoodIsFree = isFree;
            drOrder.OrderFoodRemarks = remark;
            dtOrder.Addv_OrderFoodRow(drOrder);
            this.grdOrderList.AutoGenerateColumns = false;
            grdOrderList.DataSource = dtOrder;
            this.SetOrderInfo();

        }

        private void SetOrderInfo()
        {
            decimal payAmount = 0;
            decimal freeAmount = 0;
            for (int i = 0; i < grdOrderList.Rows.Count; i++)
            {
                grdOrderList.Rows[i].Cells[0].Value = (i + 1).ToString();
                payAmount += int.Parse(grdOrderList.Rows[i].Cells[5].Value.ToString())
                    * decimal.Parse(grdOrderList.Rows[i].Cells[7].Value.ToString());

                freeAmount += (bool)grdOrderList.Rows[i].Cells[8].Value
                    ? int.Parse(grdOrderList.Rows[i].Cells[5].Value.ToString())
                        * decimal.Parse(grdOrderList.Rows[i].Cells[7].Value.ToString())
                    : 0;

                grdOrderList.Rows[i].Cells[8].ReadOnly = (bool)grdOrderList.Rows[i].Cells[8].Value;

                grdOrderList.Rows[i].DefaultCellStyle.ForeColor = (bool)grdOrderList.Rows[i].Cells[8].Value ? Color.Green : Color.Blue;
            }
            this.labAmount.Text = payAmount.ToString();
            this.labFreeAmount.Text = freeAmount.ToString();
            this.labFinalAmount.Text = (payAmount - freeAmount).ToString();
        }

        private void OrderDetail_Load(object sender, EventArgs e)
        {
            InitialTableInfo();
        //    if (有菜单)
        //    { }
        //    else
        //    { dtOrder.Rows.Clear(); }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FoodSearch fs = new FoodSearch();
            fs.ShowDialog(this);
        }

        private void InitialTableInfo()
        {
            this.labBillNum.Text = "xxxxxxx";
            this.labHeadCount.Text = this.currentTable.HeadCount;//要换成order的人数
            this.labMemberNum.Text = "xxxxxxx";
            this.labTableName.Text = this.currentTable.TableName;
            this.labTableNum.Text = this.currentTable.TableNumber;
            this.labTime.Text = DateTime.Now.ToString("yyyy-MM-dd");//yao gai
            this.labUnitPay.Text = "10.00";
            this.labWaiter.Text = "xxxxx";
        }

        protected override void OnResize(EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            base.OnResize(e);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}